package com.bytedance.frameworks.baselib.network.http.b.a;

import com.bytedance.common.utility.Logger;
import com.bytedance.retrofit2.mime.TypedInput;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

import okhttp3.MediaType;
import okhttp3.ResponseBody;

final class SsOkHttp3Response implements TypedInput {
    private final ResponseBody responseBody;
    private final boolean gzip;

    SsOkHttp3Response(ResponseBody responseBody, boolean gzip) {
        this.responseBody = responseBody;
        this.gzip = gzip;
    }

    public final String mimeType() {
        MediaType mediaType = this.responseBody.contentType();
        return mediaType == null ? null : mediaType.toString();
    }

    public final InputStream in() throws IOException {
        InputStream inputStream = this.responseBody.byteStream();
        if (this.gzip) {
            inputStream = new GZIPInputStream(inputStream);
            if (Logger.debug()) {
                Logger.v("SsOkHttp3Client", "get gzip response for file download");
            }
            return inputStream;
        } else {
            return inputStream;
        }
    }

    public final long length() throws IOException {
        return this.responseBody.contentLength();
    }
}

